
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System;
using Kakera;
using System.IO;

public class Init : MonoBehaviour {

	int cursor = 0;

	bool first = true;

	GameObject camera;

	[SerializeField]
    private Unimgpicker imagePicker;

	
	private GameObject quad;
	[SerializeField]
    private MeshRenderer rend;

	// Use this for initialization
	//todo size of window
	//button export  gif video(combine&&opti) || Search filter || toast hint
	//settings menu search
	//additional: trending pics hot   &&& mixture diejia
	//fix: button layer removal

	//hitory used  \\ fav \\ hot    roundicon menu

	//other idea  facial unlock
	//record
	void Start () {
		//DebugConsole.Log(imagePicker.Complete.ToString());
		//gameObject.GetComponent<TextMesh> ().text="ddd";
		camera = GameObject.Find("mainCam");
		camera.AddComponent<CameraFilterPack_Vision_Crystal>();

		GameObject pick = GameObject.Find("Pick");
		RectTransform btnPick = pick.GetComponent<RectTransform>();
		btnPick.sizeDelta = new Vector2(Screen.width / 6, Screen.height / 17);
		//btnPick.
		GameObject next = GameObject.Find("Next");
		RectTransform btnNext = next.GetComponent<RectTransform>();
		btnNext.sizeDelta = new Vector2(Screen.width / 6, Screen.height / 17);
	}
	
	public void OnClickChange(){
		if(first){
			Destroy(camera.GetComponent<CameraFilterPack_Vision_Crystal>());
		}
		first = false;

		if(cursor >= effects.Length){
			cursor = 0;
		}
		DebugConsole.Clear();
		DebugConsole.Log (effects[cursor], "error");
		Type efType = Type.GetType(effects[cursor]);
		//Component comp = GetComponent(effects[cursor]);
		camera.AddComponent(efType);

		int pre = cursor;
		pre--;
		if(pre < 0){
			pre = effects.Length - 1;
		}
		Destroy(camera.GetComponent(effects[pre]));
		cursor++;
	}

	void Awake()
    {
		//quad = GameObject.Find("Quad");
		//imagePicker = this.AddComponent<Unimgpicker>();
		//imagePicker = GetComponent<Unimgpicker>();
		//imagePicker = new Unimgpicker();
		//DebugConsole.Log(imagePicker.ToString());
        imagePicker.Completed += (string path) =>
        {
			//StartCoroutine
            StartCoroutine(LoadImage(path));
        };
		#if !UNITY_EDITOR
		OnWinInitSize();
		#endif
    }

	void OnWinInitSize()
	{

		Screen.SetResolution(800, 800, false);
	
	}

	public void OnSavePic(){
		StartCoroutine (GeneratePNG());
	}

	public void OnPressShowPicker(){
        imagePicker.Show("Select Image", "unimgpicker", 1024);
    }

	// Update is called once per frame
	void Update () {
		//gameObject.GetComponent<CapsuleCollider> ("capsmall").translate += new Vector3(0, -1, 0);
		//GameObject go = transform.Find ("capsmall").gameObject;
		//GameObject go = transform.Find ("Capsule").gameObject;
		//go.transform.Translate (Vector3.down);
	}

	private IEnumerator LoadImage(string path)
        {
			DebugConsole.Log(path);
            var url = "file://" + path;
            var www = new WWW(url);
            yield return www;

            var texture = www.texture;
            if (texture == null)
            {
                Debug.LogError("Failed to load texture url:" + url);
				DebugConsole.Log("Failed to load texture url:" + url);
            }

            //rend.GetComponent<Renderer>().material.mainTexture = texture;
			rend.material.mainTexture = texture;
        }

	IEnumerator GeneratePNG () {


		// 因为"WaitForEndOfFrame"在OnGUI之后执行
		// 所以我们只在渲染完成之后才读取屏幕上的画面
		yield return new WaitForEndOfFrame();

		int width = Screen.width;
		int height = Screen.height;
		// 创建一个屏幕大小的纹理，RGB24 位格（24位格没有透明通道，32位的有）
		Texture2D tex = new Texture2D (width, height, TextureFormat.RGB24, false);

		// 读取屏幕内容到我们自定义的纹理图片中
		tex.ReadPixels (new Rect(0, 0, width, height), 0, 0);
		// 保存前面对纹理的修改
		tex.Apply ();

		// 编码纹理为PNG格式
		byte[] bytes = tex.EncodeToPNG();
		// 销毁吴永的图片纹理
		Destroy (tex);



		//File.WriteAllBytes(Application.dataPath + "/onPcSavedScreen.png", bytes);
		DebugConsole.Log(Application.persistentDataPath, "error");
		File.WriteAllBytes(Application.persistentDataPath + "/savedpic.png", bytes);
		AndroidJavaClass up = new AndroidJavaClass ("com.unity3d.player.UnityPlayer");
		AndroidJavaObject cAct = up.GetStatic<AndroidJavaObject> ("currentActivity");

		AndroidJavaClass toClz = new AndroidJavaClass ("android.widget.Toast");
		AndroidJavaObject txt = toClz.CallStatic<AndroidJavaObject> ("makeText", new object[]{cAct, "Generate Ok", 1});
		txt.Call ("show");
		//AndroidJavaObject ("android.widget.Toast");
		//Debug.Log(CurrentSDCardPath());
		DebugConsole.Log(CurrentSDCardPath());

	}

	public string CurrentSDCardPath()
	{
		string m_sdCardPath = "";
			if (m_sdCardPath == null)
			{
				AndroidJavaClass jc = new AndroidJavaClass("android.os.Environment");
				IntPtr getExternalStorageDirectoryMethod = AndroidJNI.GetStaticMethodID(jc.GetRawClass(), "getExternalStorageDirectory", "()Ljava/io/File;");
				IntPtr file = AndroidJNI.CallStaticObjectMethod(jc.GetRawClass(), getExternalStorageDirectoryMethod, new jvalue[] { });
				IntPtr getPathMethod = AndroidJNI.GetMethodID(AndroidJNI.GetObjectClass(file), "getPath", "()Ljava/lang/String;");
				IntPtr path = AndroidJNI.CallObjectMethod(file,getPathMethod,new jvalue[] { });
				m_sdCardPath = AndroidJNI.GetStringUTFChars(path);
				AndroidJNI.DeleteLocalRef(file);
				AndroidJNI.DeleteLocalRef(path);
				Debug.Log("m_sdCardPath = " + m_sdCardPath);
			}
			return m_sdCardPath;
		

	}

	string[] effects = {"CameraFilterPack_3D_Anomaly",
"CameraFilterPack_3D_Binary",
"CameraFilterPack_3D_BlackHole",
"CameraFilterPack_3D_Computer",
"CameraFilterPack_3D_Distortion",
"CameraFilterPack_3D_Fog_Smoke",
"CameraFilterPack_3D_Ghost",
"CameraFilterPack_3D_Inverse",
"CameraFilterPack_3D_Matrix",
"CameraFilterPack_3D_Mirror",
"CameraFilterPack_3D_Myst",
"CameraFilterPack_3D_Scan_Scene",
"CameraFilterPack_3D_Shield",
"CameraFilterPack_3D_Snow",
"CameraFilterPack_AAA_Blood",
"CameraFilterPack_AAA_BloodOnScreen",
"CameraFilterPack_AAA_Blood_Hit",
"CameraFilterPack_AAA_Blood_Plus",
"CameraFilterPack_AAA_SuperComputer",
"CameraFilterPack_AAA_SuperHexagon",
"CameraFilterPack_AAA_WaterDrop",
"CameraFilterPack_AAA_WaterDropPro",
"CameraFilterPack_Alien_Vision",
"CameraFilterPack_Antialiasing_FXAA",
"CameraFilterPack_Atmosphere_Fog",
"CameraFilterPack_Atmosphere_Rain",
"CameraFilterPack_Atmosphere_Rain_Pro",
"CameraFilterPack_Atmosphere_Rain_Pro_3D",
"CameraFilterPack_Atmosphere_Snow_8bits",
"CameraFilterPack_Blend2Camera_Blend",
"CameraFilterPack_Blend2Camera_BlueScreen",
"CameraFilterPack_Blend2Camera_Color",
"CameraFilterPack_Blend2Camera_ColorBurn",
"CameraFilterPack_Blend2Camera_ColorDodge",
"CameraFilterPack_Blend2Camera_ColorKey",
"CameraFilterPack_Blend2Camera_Darken",
"CameraFilterPack_Blend2Camera_DarkerColor",
"CameraFilterPack_Blend2Camera_Difference",
"CameraFilterPack_Blend2Camera_Divide",
"CameraFilterPack_Blend2Camera_Exclusion",
"CameraFilterPack_Blend2Camera_GreenScreen",
"CameraFilterPack_Blend2Camera_HardLight",
"CameraFilterPack_Blend2Camera_HardMix",
"CameraFilterPack_Blend2Camera_Hue",
"CameraFilterPack_Blend2Camera_Lighten",
"CameraFilterPack_Blend2Camera_LighterColor",
"CameraFilterPack_Blend2Camera_LinearBurn",
"CameraFilterPack_Blend2Camera_LinearDodge",
"CameraFilterPack_Blend2Camera_LinearLight",
"CameraFilterPack_Blend2Camera_Luminosity",
"CameraFilterPack_Blend2Camera_Multiply",
"CameraFilterPack_Blend2Camera_Overlay",
"CameraFilterPack_Blend2Camera_PhotoshopFilters",
"CameraFilterPack_Blend2Camera_PinLight",
"CameraFilterPack_Blend2Camera_Saturation",
"CameraFilterPack_Blend2Camera_Screen",
"CameraFilterPack_Blend2Camera_SoftLight",
"CameraFilterPack_Blend2Camera_SplitScreen",
"CameraFilterPack_Blend2Camera_SplitScreen3D",
"CameraFilterPack_Blend2Camera_Subtract",
"CameraFilterPack_Blend2Camera_VividLight",
"CameraFilterPack_Blizzard",
"CameraFilterPack_Blur_Bloom",
"CameraFilterPack_Blur_BlurHole",
"CameraFilterPack_Blur_Blurry",
"CameraFilterPack_Blur_Dithering2x2",
"CameraFilterPack_Blur_DitherOffset",
"CameraFilterPack_Blur_Focus",
"CameraFilterPack_Blur_GaussianBlur",
"CameraFilterPack_Blur_Movie",
"CameraFilterPack_Blur_Noise",
"CameraFilterPack_Blur_Radial",
"CameraFilterPack_Blur_Radial_Fast",
"CameraFilterPack_Blur_Regular",
"CameraFilterPack_Blur_Steam",
"CameraFilterPack_Blur_Tilt_Shift",
"CameraFilterPack_Blur_Tilt_Shift_Hole",
"CameraFilterPack_Blur_Tilt_Shift_V",
"CameraFilterPack_Broken_Screen",
"CameraFilterPack_Colors_Adjust_ColorRGB",
"CameraFilterPack_Colors_Adjust_FullColors",
"CameraFilterPack_Colors_Adjust_PreFilters",
"CameraFilterPack_Colors_BleachBypass",
"CameraFilterPack_Colors_Brightness",
"CameraFilterPack_Colors_DarkColor",
"CameraFilterPack_Colors_HSV",
"CameraFilterPack_Colors_HUE_Rotate",
"CameraFilterPack_Colors_NewPosterize",
"CameraFilterPack_Colors_RgbClamp",
"CameraFilterPack_Colors_Threshold",
"CameraFilterPack_Color_BrightContrastSaturation",
"CameraFilterPack_Color_Chromatic_Aberration",
"CameraFilterPack_Color_Chromatic_Plus",
"CameraFilterPack_Color_Contrast",
"CameraFilterPack_Color_GrayScale",
"CameraFilterPack_Color_Invert",
"CameraFilterPack_Color_Noise",
"CameraFilterPack_Color_RGB",
"CameraFilterPack_Color_Sepia",
"CameraFilterPack_Color_Switching",
"CameraFilterPack_Color_YUV",
"CameraFilterPack_Convert_Normal",
"CameraFilterPack_Distortion_Aspiration",
"CameraFilterPack_Distortion_BigFace",
"CameraFilterPack_Distortion_BlackHole",
"CameraFilterPack_Distortion_Dissipation",
"CameraFilterPack_Distortion_Dream",
"CameraFilterPack_Distortion_Dream2",
"CameraFilterPack_Distortion_FishEye",
"CameraFilterPack_Distortion_Flag",
"CameraFilterPack_Distortion_Flush",
"CameraFilterPack_Distortion_Half_Sphere",
"CameraFilterPack_Distortion_Heat",
"CameraFilterPack_Distortion_Lens",
"CameraFilterPack_Distortion_Noise",
"CameraFilterPack_Distortion_ShockWave",
"CameraFilterPack_Distortion_ShockWaveManual",
"CameraFilterPack_Distortion_Twist",
"CameraFilterPack_Distortion_Twist_Square",
"CameraFilterPack_Distortion_Water_Drop",
"CameraFilterPack_Distortion_Wave_Horizontal",
"CameraFilterPack_Drawing_BluePrint",
"CameraFilterPack_Drawing_CellShading",
"CameraFilterPack_Drawing_CellShading2",
"CameraFilterPack_Drawing_Comics",
"CameraFilterPack_Drawing_Crosshatch",
"CameraFilterPack_Drawing_Curve",
"CameraFilterPack_Drawing_EnhancedComics",
"CameraFilterPack_Drawing_Halftone",
"CameraFilterPack_Drawing_Laplacian",
"CameraFilterPack_Drawing_Lines",
"CameraFilterPack_Drawing_Manga",
"CameraFilterPack_Drawing_Manga2",
"CameraFilterPack_Drawing_Manga3",
"CameraFilterPack_Drawing_Manga4",
"CameraFilterPack_Drawing_Manga5",
"CameraFilterPack_Drawing_Manga_Color",
"CameraFilterPack_Drawing_Manga_Flash",
"CameraFilterPack_Drawing_Manga_FlashWhite",
"CameraFilterPack_Drawing_Manga_Flash_Color",
"CameraFilterPack_Drawing_NewCellShading",
"CameraFilterPack_Drawing_Paper",
"CameraFilterPack_Drawing_Paper2",
"CameraFilterPack_Drawing_Paper3",
"CameraFilterPack_Drawing_Toon",
"CameraFilterPack_Edge_BlackLine",
"CameraFilterPack_Edge_Edge_filter",
"CameraFilterPack_Edge_Golden",
"CameraFilterPack_Edge_Neon",
"CameraFilterPack_Edge_Sigmoid",
"CameraFilterPack_Edge_Sobel",
"CameraFilterPack_EXTRA_Rotation",
"CameraFilterPack_EXTRA_SHOWFPS",
"CameraFilterPack_EyesVision_1",
"CameraFilterPack_EyesVision_2",
"CameraFilterPack_Film_ColorPerfection",
"CameraFilterPack_Film_Grain",
"CameraFilterPack_Fly_Vision",
"CameraFilterPack_FX_8bits",
"CameraFilterPack_FX_8bits_gb",
"CameraFilterPack_FX_Ascii",
"CameraFilterPack_FX_DarkMatter",
"CameraFilterPack_FX_DigitalMatrix",
"CameraFilterPack_FX_DigitalMatrixDistortion",
"CameraFilterPack_FX_Dot_Circle",
"CameraFilterPack_FX_Drunk",
"CameraFilterPack_FX_Drunk2",
"CameraFilterPack_FX_EarthQuake",
"CameraFilterPack_FX_Funk",
"CameraFilterPack_FX_Glitch1",
"CameraFilterPack_FX_Glitch2",
"CameraFilterPack_FX_Glitch3",
"CameraFilterPack_FX_Grid",
"CameraFilterPack_FX_Hexagon",
"CameraFilterPack_FX_Hexagon_Black",
"CameraFilterPack_FX_Hypno",
"CameraFilterPack_FX_InverChromiLum",
"CameraFilterPack_FX_Mirror",
"CameraFilterPack_FX_Plasma",
"CameraFilterPack_FX_Psycho",
"CameraFilterPack_FX_Scan",
"CameraFilterPack_FX_Screens",
"CameraFilterPack_FX_Spot",
"CameraFilterPack_FX_superDot",
"CameraFilterPack_FX_ZebraColor",
"CameraFilterPack_Glasses_On",
"CameraFilterPack_Glasses_On_2",
"CameraFilterPack_Glasses_On_3",
"CameraFilterPack_Glasses_On_4",
"CameraFilterPack_Glasses_On_5",
"CameraFilterPack_Glasses_On_6",
"CameraFilterPack_Glitch_Mozaic",
"CameraFilterPack_Glow_Glow",
"CameraFilterPack_Glow_Glow_Color",
"CameraFilterPack_Gradients_Ansi",
"CameraFilterPack_Gradients_Desert",
"CameraFilterPack_Gradients_ElectricGradient",
"CameraFilterPack_Gradients_FireGradient",
"CameraFilterPack_Gradients_Hue",
"CameraFilterPack_Gradients_NeonGradient",
"CameraFilterPack_Gradients_Rainbow",
"CameraFilterPack_Gradients_Stripe",
"CameraFilterPack_Gradients_Tech",
"CameraFilterPack_Gradients_Therma",
"CameraFilterPack_Light_Rainbow",
"CameraFilterPack_Light_Rainbow2",
"CameraFilterPack_Light_Water",
"CameraFilterPack_Light_Water2",
"CameraFilterPack_Lut_PlayWith",
"CameraFilterPack_Lut_Plus",
"CameraFilterPack_Lut_Simple",
"CameraFilterPack_Lut_TestMode",
"CameraFilterPack_NightVisionFX",
"CameraFilterPack_NightVision_4",
"CameraFilterPack_Noise_TV",
"CameraFilterPack_Noise_TV_2",
"CameraFilterPack_Noise_TV_3",
"CameraFilterPack_Oculus_NightVision1",
"CameraFilterPack_Oculus_NightVision2",
"CameraFilterPack_Oculus_NightVision3",
"CameraFilterPack_Oculus_NightVision5",
"CameraFilterPack_Oculus_ThermaVision",
"CameraFilterPack_OldFilm_Cutting1",
"CameraFilterPack_OldFilm_Cutting2",
"CameraFilterPack_Pixelisation_Dot",
"CameraFilterPack_Pixelisation_OilPaint",
"CameraFilterPack_Pixelisation_OilPaintHQ",
"CameraFilterPack_Pixelisation_Sweater",
"CameraFilterPack_Pixel_Pixelisation",
"CameraFilterPack_Real_VHS",
"CameraFilterPack_Retro_Loading",
"CameraFilterPack_Sharpen_Sharpen",
"CameraFilterPack_Special_Bubble",
"CameraFilterPack_TV_50",
"CameraFilterPack_TV_80",
"CameraFilterPack_TV_ARCADE",
"CameraFilterPack_TV_ARCADE_2",
"CameraFilterPack_TV_ARCADE_Fast",
"CameraFilterPack_TV_Artefact",
"CameraFilterPack_TV_BrokenGlass",
"CameraFilterPack_TV_BrokenGlass2",
"CameraFilterPack_TV_Chromatical",
"CameraFilterPack_TV_Chromatical2",
"CameraFilterPack_TV_CompressionFX",
"CameraFilterPack_TV_Distorted",
"CameraFilterPack_TV_Horror",
"CameraFilterPack_TV_LED",
"CameraFilterPack_TV_MovieNoise",
"CameraFilterPack_TV_Noise",
"CameraFilterPack_TV_Old",
"CameraFilterPack_TV_Old_Movie",
"CameraFilterPack_TV_Old_Movie_2",
"CameraFilterPack_TV_PlanetMars",
"CameraFilterPack_TV_Posterize",
"CameraFilterPack_TV_Rgb",
"CameraFilterPack_TV_Tiles",
"CameraFilterPack_TV_Vcr",
"CameraFilterPack_TV_VHS",
"CameraFilterPack_TV_VHS_Rewind",
"CameraFilterPack_TV_Video3D",
"CameraFilterPack_TV_Videoflip",
"CameraFilterPack_TV_Vignetting",
"CameraFilterPack_TV_Vintage",
"CameraFilterPack_TV_WideScreenCircle",
"CameraFilterPack_TV_WideScreenHorizontal",
"CameraFilterPack_TV_WideScreenHV",
"CameraFilterPack_TV_WideScreenVertical",
"CameraFilterPack_VHS_Tracking",
"CameraFilterPack_Vision_Aura",
"CameraFilterPack_Vision_AuraDistortion",
"CameraFilterPack_Vision_Blood",
"CameraFilterPack_Vision_Blood_Fast",
"CameraFilterPack_Vision_Crystal",
"CameraFilterPack_Vision_Drost",
"CameraFilterPack_Vision_Hell_Blood",
"CameraFilterPack_Vision_Plasma",
"CameraFilterPack_Vision_Psycho",
"CameraFilterPack_Vision_Rainbow",
"CameraFilterPack_Vision_SniperScore",
"CameraFilterPack_Vision_Tunnel",
"CameraFilterPack_Vision_Warp",
"CameraFilterPack_Vision_Warp2"};
}
